package com.bawei.yuekao;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import com.bawei.yuekao.adapter.MainFragmentAdapter;
import com.bawei.yuekao.fragment.LocalMusicFragment;
import com.bawei.yuekao.fragment.LoveMusicFragment;
import com.bawei.yuekao.fragment.NetMusicFragment;
import com.bawei.yuekao.fragment.TuKuFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 引导页
 */
public class MainActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<Fragment> list;
    private List<String> title;
    private MainFragmentAdapter mainFragmentAdapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{
                    "android.permission.INTERNET",
                    "android.permission.WRITE_EXTERNAL_STORAGE",
                    "android.permission.READ_EXTERNAL_STORAGE",
                    "android.permission.READ_CONTACTS",
                    "android.permission.READ_SMS",
                    "android.permission.CALL_PHONE",
                    "android.permission.READ_CALL_LOG",
                    "android.permission.WRITE_CALL_LOG",
                    "android.permission.ACCESS_NETWORK_STATE",
                    "android.permission.READ_PHONE_STATE"

            }, 101);

        }
    }
    //权限授予成功后加载布局

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 101 && grantResults != null){
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED ){//授予成功
                tabLayout = findViewById(R.id.tab);
                viewPager = findViewById(R.id.view_pager);
                list = new ArrayList<>();
                list.add(new LocalMusicFragment());
                list.add(new NetMusicFragment());
                list.add(new LoveMusicFragment());
                list.add(new TuKuFragment());
                title = new ArrayList<>();
                title.add("本地");
                title.add("网络");
                title.add("收藏");
                title.add("图库");
                mainFragmentAdapter = new MainFragmentAdapter(getSupportFragmentManager(), list, title);
                viewPager.setAdapter(mainFragmentAdapter);
                tabLayout.setupWithViewPager(viewPager);
            }else{
                Toast.makeText(this, "授予失败", Toast.LENGTH_SHORT).show();
            }

        }

    }
}
